package be.webchess.www.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import be.webchess.www.dao.UserDAO;
import be.webchess.www.exceptions.ChessException;
import be.webchess.www.model.User;
import be.webchess.www.service.IUserService;
import be.webchess.www.utils.MessageDigestHelper;

public class UserService extends GenericService<User> implements IUserService {

	public UserService ( Connection con )
	{
		dao = new UserDAO(con);
	}
	
	@Override
	public boolean doLogin(User user) throws ChessException {

		try {			
			User		u = this.findByUsername(user.getLogin());
			
			if ( u == null )
				return false;
			
			if ( user.getPassword().compareTo(u.getPassword()) == 0 )
				return true;
			
		} catch ( Exception ex )
		{
		}
		
		return false;
	}

	@Override
	public User findByUsername(String username) throws ChessException {
		try {
			return ((UserDAO)dao).findByUsername(username);
		} catch ( SQLException ex )
		{
			try { 
				dao.rollback();			
			}catch ( SQLException ex2 )
			{				
			}
			
			throw new ChessException(ex);
		}

	}



}
